using System;
using System.ServiceModel;
using System.Windows;
using GalaSoft.MvvmLight;

namespace Locator.Silverlight.Client.Models
{
    public abstract class ModelBase<TService> where TService : class
    {
        protected TService Service { get; set; }

        private ModelBase()
        {

        }

        protected ModelBase(TService service)
        {
            if (!ViewModelBase.IsInDesignModeStatic)
            {
                this.Service = service;
                IHasEndpoint serviceAsClientBase = service as IHasEndpoint;
                if (serviceAsClientBase != null)
                {
                    string servicePath = serviceAsClientBase.Endpoint.Address.Uri.LocalPath;
                    string appHostUrl = Application.Current.Host.Source.Host;
                    int appHostPort = Application.Current.Host.Source.Port;
                    string newServiceUrl = String.Format("http://{0}:{1}{2}", appHostUrl, appHostPort, servicePath);
                    serviceAsClientBase.Endpoint.Address = new EndpointAddress(newServiceUrl);
                }
            }
        }
    }
}
